﻿using System;
using compshop.Infrastructure.CompShopDataService;
using System.Collections.Generic;
using System.Linq;

namespace compshop.Infrastructure
{
    public static class CompShopEntitiesContext
    {
        static private CompShopEntities compShopDataContext;

        public static CompShopEntities GetInstance()
        {
            if (compShopDataContext == null)
            {
                return compShopDataContext = new CompShopEntities(new Uri(Resource.ServiceURI));
            }
            else
            {
                return compShopDataContext;
            }
        }

        public static int Save()
        {
            int res = 0;
            try
            {
                GetInstance().SaveChanges();
                return res;
            }
            catch (Exception ex)
            {
                return -1;
            }
        }


        #region Store Procedures

        public static int? AddGoods(int genProductID, int count, int department, System.DateTime shipDate, System.Decimal purchasePrice)
        {
            string strParams = String.Format("?genProductID={0}&count={1}&department={2}&shipDate={3}&purchasePrice={4}",
                genProductID.ToString(), count.ToString(), department.ToString(), "datetime'"+DateTime.Now.ToString("s")+"'",
                purchasePrice.ToString());
            int? res = 0;
            try
            {
                string str = Resource.AddProductFromSupplierURI + strParams;
                res = GetInstance().Execute<int>(new Uri(str)).First();
            }
            catch (Exception e)
            {
                return -1;
            }
            return res;
        }

        public static int AddProductToOrder(int orderID, int productID, int count)
        {
            string strParams = String.Format("?orderID={0}&productID={1}&count={2}",
                orderID.ToString(), productID.ToString(), count.ToString());
            int res = 0;
            try
            {
                string str = Resource.AddProductToOrderURI + strParams;
                res = GetInstance().Execute<int>(new Uri(str)).First();
            }
            catch (Exception e)
            {
                return -1;
            }
            return res;
        }


        #endregion
    }
}
